package com.ruoyi.project.comp.customer;


import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.AuthorizationUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.post.service.IPostService;
import com.ruoyi.project.system.role.domain.Role;
import com.ruoyi.project.system.role.service.IRoleService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;

/**
 * 客户信息管理
 *
 * @author ruoyi
 */
@Controller
@RequestMapping("/comp/customer")
public class CustomerController extends BaseController {
    private String prefix = "comp/customer";

    @Autowired
    private IUserService userService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPostService postService;

    /**
     * 跳转客户列表页面
     */
    @GetMapping()
    public String customer() {
        return prefix + "/customer";
    }

    /**
     * 客户列表查询（默认筛选usertype=03的客户数据）
     */
    // @RequiresPermissions("comp:customer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(User user) {
        // 核心：强制筛选客户类型为02，无需前端传递
        startPage();
        List<User> list = userService.selectCustomerList(user);
        return getDataTable(list);
    }


    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(User user) {
        // 1. 校验权限
//        userService.checkUserAllowed(user);
//        userService.checkUserDataScope(user.getUserId());
        // 2. 校验该记录是否为管理员（usertype=01）
        User admin = userService.selectUserById(user.getUserId());
        if (admin == null || !"02".equals(admin.getUserType())) {
            return error("操作失败，仅允许修改管理员状态");
        }
        // 3. 执行状态修改，强制保留管理员类型
        user.setUserType("02");
        return toAjax(userService.changeStatus(user));
    }


    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(userService.deleteUserByIds(ids));
    }


}
